# app/config/base_setting.py
import os
from dotenv import load_dotenv

import logging
from logging.handlers import RotatingFileHandler

# Resolve the backend root reliably no matter where Python is launched
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
APP_ROOT = os.path.abspath(os.path.join(BASE_DIR, ".."))
PROJECT_ROOT  = os.path.abspath(os.path.join(APP_ROOT, ".."))

# Step 1: Load .env file from the backend root
load_dotenv(os.path.join(PROJECT_ROOT, ".env"))
ENV = os.getenv("ENV", "development")

# Now define your folders relative to PROJECT_ROOT
UPLOAD_FOLDER = os.path.join(PROJECT_ROOT, 'uploads')
ARCHIVE_UPLOAD_FOLDER = os.path.join(PROJECT_ROOT, 'archive', 'upload')

OCR_OUTPUT_FOLDER = os.path.join(PROJECT_ROOT, 'ocred')
ARCHIVE_OCR_OUTPUT_FOLDER = os.path.join(PROJECT_ROOT, 'archive', 'ocred')

EXCEL_OUTPUT_FOLDER = os.path.join(PROJECT_ROOT, 'excel_files')
ARCHIVE_EXCEL_OUTPUT_FOLDER = os.path.join(PROJECT_ROOT, 'archive', 'excel_files')

# Ensure all folders exist at startup
for folder in [UPLOAD_FOLDER, OCR_OUTPUT_FOLDER, ARCHIVE_UPLOAD_FOLDER, ARCHIVE_OCR_OUTPUT_FOLDER, EXCEL_OUTPUT_FOLDER, ARCHIVE_EXCEL_OUTPUT_FOLDER]:
    os.makedirs(folder, exist_ok=True)
    

DB_PATH = os.path.join(APP_ROOT, "data", "users.db")
PRIVATE_KEY_PATH = os.path.join(APP_ROOT, "keys", "id_rsa")

SECRET_KEY = "change-this-secret-key-in-production"
ALLOWED_ORIGINS = [
    "http://localhost:5173",
    "http://localhost",
]

# Step 2:  SSH Settings
PRIVATE_KEY_PATH = os.path.join(PROJECT_ROOT , 'keys', 'id_rsa')
PRIVATE_KEY_PASS = os.getenv("PRIVATE_KEY_PASS")
REMOTE_HOSTS = [
    {
        "host": "connect.nmb1.seetacloud.com",
        "port": 34074,
        "username": "root"
    },
    {
        "host": "connect.nmb1.seetacloud.com",
        "port": 21382,
        "username": "root"
    },
    {
        "host": "connect.westb.seetacloud.com",
        "port": 45754,
        "username": "root"
    },
    {
        "host": "connect.nmb1.seetacloud.com",
        "port": 28211,
        "username": "root"
    },
    {
        "host": "connect.yza1.seetacloud.com",
        "port": 55717,
        "username": "root"
    }
]
REMOTE_INPUT_DIR = "/root/PaddleOCR/input_img/"
REMOTE_OUTPUT_DIR = "/root/PaddleOCR/output_img/"
REMOTE_ARCHIVE_DIR = "/root/PaddleOCR/archive_img/"


# Step 3:  Setup log folder
LOG_DIR = os.path.join(APP_ROOT, "logs")
os.makedirs(LOG_DIR, exist_ok=True)

# --- Backend Logger ---
back_log_path = os.path.join(LOG_DIR, "backend.log")
backend_logger = logging.getLogger("backend_logger")
backend_logger.setLevel(logging.DEBUG)

backend_handler = RotatingFileHandler(back_log_path, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8')
formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] %(name)s: %(message)s")
backend_handler.setFormatter(formatter)

if not backend_logger.hasHandlers():
    backend_logger.addHandler(backend_handler)

# --- Frontend Logger ---
frontend_logger = logging.getLogger("frontend_logger")
frontend_logger.setLevel(logging.DEBUG)

frontend_handler = RotatingFileHandler(
    os.path.join(LOG_DIR, "frontend.log"),
    maxBytes=5 * 1024 * 1024,
    backupCount=3,
    encoding="utf-8",
)
frontend_handler.setFormatter(formatter)

if not frontend_logger.hasHandlers():
    frontend_logger.addHandler(frontend_handler)
  